import unittest
from django.db import transaction

from sirogi.users.models import SirogiUser
from django.test.client import Client


class BasicTestCase(unittest.TestCase):
    
    def test_non_existant_user_cannot_login(self):
        """
        This test will check that a non existant user cannot login to the site
        """
        c = Client()
        self.failUnlessEqual(c.login(username='nonexistant', password='nones'), False)

    def test_users_belong_to_only_one_group(self):
        """
        This test will check that users from site belong to one and only
        one group
        """
        users = SirogiUser.objects.all()
        for user in users:
            self.failUnlessEqual(len(user.groups.all()),1)
            
    def test_user_roles_match_groups(self):
        """
        This test will check that the users role are matched correctly with
        a group with the same name
        """
        users = SirogiUser.objects.all()
        for user in users:
            self.failUnlessEqual(user.role == user.groups.get().name, True)

    def test_NIC_over_char(self):
        """
        This test will check that the field do not allow the user to enter
        more than 15 characters
        """
        try:
            user = SirogiUser.objects.create(username='test_user', password='password', NIC='1234567890123456')
            user.save()
        except:
            transaction.commit()

        users = SirogiUser.objects.filter(username='test_user')
        self.failUnlessEqual(len(users), 0)
